package mysql

import (
	"errors"
	"gitee.com/DonHz/qimi/bluebell/models"
	"go.uber.org/zap"
	"gorm.io/gorm"
)

func GetCommunityList() ([]*models.Community, error) {
	var commuList []*models.Community
	err:=db.Select("community_id, community_name").Find(&commuList).Error
	if err!=nil {
		zap.L().Error("获取社区分类数据失败", zap.Error(err))
		if err==gorm.ErrRecordNotFound {
			return nil, errors.New("there is no community in db")
		}
		return nil, err
	}
	return commuList, nil
}

func GetCommunityById(id int64) (*models.Community, error) {
	var community models.Community
	err:=db.Select("community_id, community_name, introduction, update_time").
		Where("id=?", id).Find(&community).Error
	if err!=nil {
		zap.L().Error("获取详细社区数据失败",
			zap.Int("id", int(id)),
			zap.Error(err))
		if err==gorm.ErrRecordNotFound {
			return &models.Community{}, errors.New("无效的id")
		}
		return &models.Community{}, err
	}
	return &community, nil
}
